package com.andoridtools.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.andoridtools.R;

import java.util.ArrayList;
import java.util.List;

public class DataDoublePickerDialog extends Dialog {

    private Params params;

    public DataDoublePickerDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void setParams(DataDoublePickerDialog.Params params) {
        this.params = params;
    }


    public void setSelection(String itemValue) {
        if (params.provinces.size() > 0) {
            int idx = params.provinces.indexOf(itemValue);
            if (idx >= 0) {
                params.initSelectionProvince = idx;
                params.loopProvinces.setCurrentItem(params.initSelectionProvince);
            }
        }
    }

    public interface OnDataSelectedListener {
        void onDatasSelected(String provincesValue, int provincesPosition, String cityValue, int citysPosition);

        void onLoopLinstener(LoopView loopView, int item);

        void onCancel();
    }

    private static final class Params {
        private boolean shadow = true;
        private boolean canCancel = true;
        private LoopView loopProvinces;
        private LoopView loopCitys;
        private String title;
        private String unit;
        private LoopListener provinceLoopListener;
        private LoopListener cityLoopListener;
        private int initSelectionProvince;
        private int initSelectionCity;
        private OnDataSelectedListener callback;
        private final List<String> provinces = new ArrayList<>();
        private final List<String> citys = new ArrayList<>();
    }

    public static class Builder {
        private final Context context;
        private final DataDoublePickerDialog.Params params;

        public Builder(Context context) {
            this.context = context;
            params = new DataDoublePickerDialog.Params();
        }

        private final String getCurrProvincesValue() {
            return params.loopProvinces.getCurrentItemValue();
        }

        private final String getCurrCitysValue() {
            return params.loopCitys.getCurrentItemValue();
        }

        public Builder setProvincesData(List<String> dataList) {
            params.provinces.clear();
            params.provinces.addAll(dataList);
            return this;
        }

        public Builder setCitysData(List<String> dataList) {
            params.citys.clear();
            params.citys.addAll(dataList);
            return this;
        }

        public Builder setProvinLoopListener(LoopListener provinceLoopListener) {
            params.provinceLoopListener = provinceLoopListener;
            return this;
        }

        public Builder setCityLoopListener(LoopListener cityLoopListener) {
            params.cityLoopListener = cityLoopListener;
            return this;
        }

        public Builder setTitle(String title) {
            params.title = title;
            return this;
        }

        public Builder setUnit(String unit) {
            params.unit = unit;
            return this;
        }

        public Builder setSelectionProvince(int selection) {
            params.initSelectionProvince = selection;
            return this;
        }

        public Builder setSelectionCity(int selection) {
            params.initSelectionCity = selection;
            return this;
        }


        public Builder setOnDataSelectedListener(OnDataSelectedListener onDataSelectedListener) {
            params.callback = onDataSelectedListener;
            return this;
        }


        public DataDoublePickerDialog create() {
            final DataDoublePickerDialog dialog = new DataDoublePickerDialog(context, params.shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
            View view = LayoutInflater.from(context).inflate(R.layout.layout_picker_double_data, null);

            if (!TextUtils.isEmpty(params.title)) {
                TextView txTitle = (TextView) view.findViewById(R.id.tx_title);
                txTitle.setText(params.title);
                txTitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        params.callback.onCancel();
                    }
                });
            }
            if (!TextUtils.isEmpty(params.unit)) {
                TextView tx_unit_province = (TextView) view.findViewById(R.id.tx_unit_province);
                tx_unit_province.setText(params.unit);
            }

            final LoopView loopProvinces = (LoopView) view.findViewById(R.id.loop_provinces);
            loopProvinces.setArrayList(params.provinces);
            loopProvinces.setNotLoop();
            if (params.provinces.size() > 0) {
                loopProvinces.setCurrentItem(params.initSelectionProvince);
            }

            final LoopView loopCitys = (LoopView) view.findViewById(R.id.loop_citys);
            loopCitys.setArrayList(params.citys);
            loopCitys.setNotLoop();
            if (params.citys.size() > 0) {
                loopCitys.setCurrentItem(params.initSelectionCity);
            }

            loopProvinces.setListener(new LoopListener() {
                @Override
                public void onItemSelect(int item) {
                    params.callback.onLoopLinstener(loopCitys, item);
                }
            });
            view.findViewById(R.id.tx_finish).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    params.callback.onDatasSelected(getCurrProvincesValue(), loopProvinces.getCurrentItem(), getCurrCitysValue(), loopCitys.getCurrentItem());
                }
            });

            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setGravity(Gravity.BOTTOM);
            win.setWindowAnimations(R.style.Animation_Bottom_Rising);

            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(params.canCancel);
            dialog.setCancelable(params.canCancel);

            params.loopProvinces = loopProvinces;
            params.loopCitys = loopCitys;
            dialog.setParams(params);
            return dialog;
        }
    }
}
